import { DataSource } from 'typeorm';
import { logger } from '../utils/logger';
import { User } from '../entities/User';
import { Chat } from '../entities/Chat';
import { ChatGroup } from '../entities/ChatGroup';
import { GroupMember } from '../entities/GroupMember';
import { HotTopic } from '../entities/HotTopic';
import { Recommendation } from '../entities/Recommendation';

export const AppDataSource = new DataSource({
  type: 'mysql',
  host: process.env.MYSQL_HOST,
  port: parseInt(process.env.MYSQL_PORT || '3306'),
  username: process.env.MYSQL_USERNAME,
  password: process.env.MYSQL_PASSWORD,
  database: process.env.MYSQL_DATABASE,
  synchronize: process.env.NODE_ENV === 'development', // 开发环境下自动同步
  // logging: process.env.NODE_ENV === 'development',
  entities: ['src/entities/**/*.ts'],
  // entities: [
  //   User, Chat, ChatGroup, GroupMember, HotTopic, Recommendation
  // ],
  migrations: [],
  subscribers: [],
});

export const initializeDatabase = async () => {
  try {
    await AppDataSource.initialize();
    logger.info('数据库连接完成~');
  } catch (error) {
    logger.error('数据库连接失败:', error);
    process.exit(1);
  }
}; 